package com.mj.hotel.mvp.ui.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.mj.hotel.R;
import com.mj.hotel.mvp.ui.event.ChangeFragmentMsg;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

public class HomeFragment extends BaseFragment {

    @BindView(R.id.homeRcv)
    public RecyclerView homeRcv;
    private ArrayList<ActionEntity> list;

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        list = new ArrayList<>();
        list.add(new ActionEntity("订房",R.drawable.iconmonstr));
        list.add(new ActionEntity("退房",R.drawable.iconmonstr));
        list.add(new ActionEntity("A房",R.drawable.iconmonstr));
        list.add(new ActionEntity("B房",R.drawable.iconmonstr));
        list.add(new ActionEntity("C房",R.drawable.iconmonstr));
        list.add(new ActionEntity("D房",R.drawable.iconmonstr));

        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        homeRcv.setLayoutManager(layoutManager);
        homeRcv.addItemDecoration(new ActionItemDecoration(3, 30, false));
        homeRcv.setHasFixedSize(true);
        homeRcv.setAdapter(new ActionRcvAdapter(list));

    }

    @Override
    public void setData(@Nullable Object data) {

    }

    class ActionRcvViewHolder extends RecyclerView.ViewHolder {
        View fruitView;
        TextView actionName;
        ImageView actionIcon;
        public ActionRcvViewHolder(View view) {
            super(view);
            fruitView = view;
            actionName = view.findViewById(R.id.actionInfo);
            actionIcon = view.findViewById(R.id.actionIcon);
        }
    }

    class ActionRcvAdapter extends RecyclerView.Adapter<ActionRcvViewHolder>{

        private ArrayList<ActionEntity> mList;

        public ActionRcvAdapter(ArrayList<ActionEntity> mList){
            this.mList = mList;
        }

        @NonNull
        @Override
        public ActionRcvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.action_item, parent, false);
            return new ActionRcvViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ActionRcvViewHolder holder, int position) {
            ActionEntity entity = mList.get(position);
            holder.actionIcon.setImageResource(entity.getIconId());
            holder.actionName.setText(entity.getInfo());
            holder.fruitView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    if(entity.info.equals("订房")){
                        EventBus.getDefault().post(ChangeFragmentMsg.getInstance(RoomListFragment.class.getSimpleName()));
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    class ActionItemDecoration extends RecyclerView.ItemDecoration{
        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public ActionItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }
    class ActionEntity{
        private String info;
        private int iconId;
        public ActionEntity(String info,int id){
            this.iconId = id;
            this.info = info;
        }

        public String getInfo() {
            return info;
        }

        public int getIconId() {
            return iconId;
        }
    }
}
